<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>{{number}}.{{title}}</title>
    <!-- 引入ACE插件 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ace.js" type="text/javascript" charset="utf-8"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ace/1.2.6/ext-language_tools.js" type="text/javascript"
        charset="utf-8"></script>
    <!-- 引入jquery CDN -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html,
        body {
            width: 100%;
            height: 100%;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f5f5f5;
        }

        .container .navbar {
            width: 100%;
            height: 60px;
            background-color: #2c3e50;
            overflow: hidden;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .container .navbar a {
            display: inline-block;
            width: 100px;
            color: white;
            font-size: 16px;
            line-height: 60px;
            text-decoration: none;
            text-align: center;
            transition: background-color 0.3s ease;
        }

        .container .navbar a:hover {
            background-color: #34495e;
        }

        .container .navbar .login {
            float: right;
        }

        .container .part1 {
            width: 100%;
            height: 600px;
            overflow: hidden;
            margin-top: 20px;
        }

        .container .part1 .left_desc {
            width: 50%;
            height: 600px;
            float: left;
            overflow: scroll;
            background-color: white;
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            margin-right: 10px;
        }

        .container .part1 .left_desc h3 {
            color: #2c3e50;
            font-size: 24px;
            margin-bottom: 15px;
        }

        .container .part1 .left_desc pre {
            font-size: 16px;
            line-height: 1.6;
            color: #555;
            white-space: pre-wrap;
            word-wrap: break-word;
        }

        .container .part1 .right_code {
            width: calc(50% - 10px);
            height: 600px;
            float: right;
            background-color: white;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
        }

        .container .part1 .right_code .ace_editor {
            height: 600px;
            border-radius: 8px;
        }

        .container .part2 {
            width: 100%;
            overflow: hidden;
            margin-top: 20px;
        }

        .container .part2 .result {
            width: calc(100% - 140px);
            float: left;
            background-color: white;
            padding: 20px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            border-radius: 8px;
            margin-right: 10px;
        }

        .container .part2 .result pre {
            font-size: 16px;
            color: #333;
        }

        .container .part2 .btn-submit {
            width: 120px;
            height: 50px;
            font-size: 16px;
            float: right;
            background-color: #26bb9c;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .container .part2 .btn-submit:hover {
            background-color: #1f9c81;
        }
    </style>
</head>

<body>
    <div class="container">
        <!-- 导航栏 -->
        <div class="navbar">
            <a href="/">首页</a>
            <a href="/All_Questions">题库</a>
            <a href="/">竞赛</a>
            <a href="/">讨论</a>
            <a href="/">求职</a>
            <a class="login" href="#">登录</a>
        </div>
        <!-- 左右呈现，题目描述和预设代码 -->
        <div class="part1">
            <div class="left_desc">
                <h3><span id="number">{{number}}</span>.{{title}}_{{star}}</h3>
                <pre>{{desc}}</pre>
            </div>
            <div class="right_code">
                <pre id="code" class="ace_editor"><textarea class="ace_text-input">{{pre_code}}</textarea></pre>
            </div>
        </div>
        <!-- 提交并且得到结果，并显示 -->
        <div class="part2">
            <div class="result"></div>
            <button class="btn-submit" onclick="submit()">提交代码</button>
        </div>
    </div>
    <script>
        // 初始化ACE编辑器
        var editor = ace.edit("code");
        editor.setTheme("ace/theme/monokai");
        editor.session.setMode("ace/mode/c_cpp");
        editor.setFontSize(16);
        editor.getSession().setTabSize(4);
        editor.setReadOnly(false);

        // 启用提示菜单
        ace.require("ace/ext/language_tools");
        editor.setOptions({
            enableBasicAutocompletion: true,
            enableSnippets: true,
            enableLiveAutocompletion: true
        });

        function submit() {
            // 收集代码和题号
            var code = editor.getSession().getValue();
            var number = $("#number").text();
            var judge_url = "/judge/" + number;

            // 发起AJAX请求
            $.ajax({
                method: 'POST',
                url: judge_url,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify({
                    'code': code,
                    'input': ''
                }),
                success: function (data) {
                    show_result(data);
                }
            });
        }

        function show_result(data) {
            var result_div = $(".container .part2 .result");
            result_div.empty();

            var _status = data.status;
            var _reason = data.reason;

            var reason_lable = $("<p>", {
                text: _reason
            });
            reason_lable.appendTo(result_div);

            if (status == 0) {
                var _stdout = data.stdout;
                var _stderr = data.stderr;

                var stdout_lable = $("<pre>", {
                    text: _stdout
                });

                var stderr_lable = $("<pre>", {
                    text: _stderr
                });

                stdout_lable.appendTo(result_div);
                stderr_lable.appendTo(result_div);
            }
        }
    </script>
</body>

</html>